<?php

namespace App\Service;

use App\Repositories\MemberCardRepository;
use App\Repositories\OrdersRepository;
use App\Repositories\UserCardRepository;
use App\Repositories\UserCourseRepository;
use App\Transformers\School\ShowTransformer;
use Illuminate\Support\Arr;

class UserCardServer extends BaseService {
    protected $fractal;
    protected $userCourseRepository;
    protected $showTransformer;
    protected $userCardRepository;
    protected $ordersRepository;
    protected $memberCardRepository;
    protected $weChatService;

    public function __construct(
        UserCourseRepository $userCourseRepository,
        UserCardRepository $userCardRepository,
        OrdersRepository $ordersRepository,
        MemberCardRepository $memberCardRepository,
        ShowTransformer $showTransformer,
        WeChatService $weChatService
    ){
        $this->userCourseRepository = $userCourseRepository;
        $this->userCardRepository = $userCardRepository;
        $this->showTransformer = $showTransformer;
        $this->ordersRepository = $ordersRepository;
        $this->memberCardRepository = $memberCardRepository;
        $this->weChatService = $weChatService;
    }

    /**
     * 选卡选课程
     * @param $params
     * @return mixed
     */
    public function store($params) {
        $uid = session('uid');
        $data = array_merge($params, ['user_id' => $uid]);
        \DB::transaction(function () use($data, &$payParams) {
            $cardNum = buildOrderNo();
            //新增用户课程记录
            $this->userCourseRepository->store(Arr::only($data, ['user_id', 'course_id']));
            $memberCardInfo = $this->memberCardRepository->show($data['card_id']);
            $cardMoney = $memberCardInfo->member_card_price + $memberCardInfo->recharge_ticket_price;
            //新增用户卡记录
            $this->userCardRepository->store([
                'user_id' => $data['user_id'],
                'member_card_id' => $data['card_id'],
                'card_number' => $cardNum,
                'total_money' => $cardMoney,
                'now_money' => $cardMoney,
                'give_money' => $memberCardInfo->recharge_ticket_price,
            ]);
            //新增订单记录
            $orderSn = buildOrderNo();
            $this->ordersRepository->store([
                'order_sn' => $orderSn,
                'order_type' => 0,
                'user_id' => $data['user_id'],
                'card_id' => $data['card_id'],
                'price' => $memberCardInfo->member_card_price,
            ]);
            $payParams = $this->weChatService->getPayParams($orderSn, $memberCardInfo->member_card_price, $data['openid']);
        });
        return responseData('', '', ['payParams' => $payParams]);
    }

    /**
     * 会员卡升级
     * @param $params
     */
    public function upgrade($params) {
        //查询用户当前会员卡信息
        \DB::transaction(function () use($params,&$payParams) {
            $uid = session('uid');
            $userCardInfo = $this->userCardRepository->showByWhere(['user_id' => $uid, 'status'=>1], ['memberCard']);
            $newMemberCardInfo = $this->memberCardRepository->show($params['new_card_id']);
            if ($userCardInfo && $newMemberCardInfo) {
                $orderSn = buildOrderNo();
                $totalMoney = $newMemberCardInfo->member_card_price - $userCardInfo->member_card_price;
                //创建升级卡订单
                $orderInfo = $this->ordersRepository->store([
                    'order_sn' => $orderSn,
                    'order_type' => 1,
                    'user_id' => $uid,
                    'card_id' => $params['new_card_id'],
                    'price' => $totalMoney,
                    'remark' => $userCardInfo->member_card_id //升级会员卡将原来卡ID存入remark
                ]);
                $this->userCardRepository->store([
                    'user_id' => $uid,
                    'member_card_id' => $params['new_card_id'],
                    'card_number' => buildOrderNo(),
                    'card_pid' => $userCardInfo->card_id,
                    'total_money' => $userCardInfo->member_card_price + $newMemberCardInfo->member_card_price +$userCardInfo->recharge_ticket_price + $newMemberCardInfo->recharge_ticket_price,
                    'now_money' => $userCardInfo->now_money + $totalMoney + $newMemberCardInfo->recharge_ticket_price,
                    'give_money' => $userCardInfo->recharge_ticket_price + $newMemberCardInfo->recharge_ticket_price,
                ]);
                //获取支付参数
                $payParams = $this->weChatService->getPayParams($orderSn, $totalMoney, $params['openid']);
            }
        });
        return responseData('','',['pay_params' => $payParams]);
    }
}
